package com.lighting.common.feign.config;

import com.lighting.common.exception.AbstractExceptionHandler;
import com.lighting.common.core.vo.BusinessException;
import feign.FeignException;
import feign.codec.DecodeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class FeignExceptionHandler extends AbstractExceptionHandler {

    @ExceptionHandler(value = FeignException.class)
    public ResponseEntity<?> handlerException(FeignException e) {
        if (e instanceof DecodeException && e.getCause() != null && e.getCause() instanceof BusinessException) {
            return this.handlerException((BusinessException) e.getCause());
        }
        return super.handlerThrowable(e);
    }

}